package com.teamone.etl.taskmanager.controller;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageInfo;
import com.teamone.etl.exception.CustomException;
import com.teamone.etl.taskmanager.entity.EtlTaskInfo;
import com.teamone.etl.taskmanager.feign.DataSourceFeignService;
import com.teamone.etl.taskmanager.service.EtlTaskInfoService;
import com.teamone.etl.taskmanager.service.ParseProcessJsonService;
import com.teamone.etl.util.ThreadPool;
import io.lettuce.core.dynamic.annotation.Param;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 任务信息表(EtlTaskInfo)表控制层

 @author makejava
 @since 2021-01-08 20:42:01 */
@RestController
@RequestMapping("/etlTaskInfo")
public class EtlTaskInfoController extends ApiController {
    /**
     服务对象
     */
    @Resource
    private EtlTaskInfoService etlTaskInfoService;

    @Resource
    private DataSourceFeignService dataSourceFeignService;

    @Resource
    private ParseProcessJsonService parseProcessJsonService;

    /**
     * 线程池
     */
    private static final ThreadPool THREAD_POOL = new ThreadPool();

    private static final int STATUS = 2;

    @GetMapping("/getAllDataSource")
    public R<List<Object>> getAllDataSource (){
        return success(dataSourceFeignService.selectAllNotDisabled());
    }

    @GetMapping("/getAllDataBaseByDataSourceId/{dataSourceId}")
    public R<List<String>> getAllDataBaseByDataSourceId(@PathVariable("dataSourceId") Integer id){
        return success(dataSourceFeignService.getAllDataBaseByDataSourceId(id));
    }

    @GetMapping("/getAllTableByDataBaseName/{dataSourceId}")
    public R<List<String>> getAllTableByDataBaseName (@PathVariable("dataSourceId") Integer id,
                                        @RequestParam("dataBaseName") String dataBaseName){
        return success(dataSourceFeignService.getAllTableByDataBaseName(id,dataBaseName));
    }

    @GetMapping("/getAllColumnByTableName/{dataSourceId}")
    public R<Map<String, List<String>>> getAllColumnByTableName (@PathVariable("dataSourceId") Integer id,
                                                                 @RequestParam("tableName") String tableName,
                                                                 @RequestParam("dataBaseName") String dataBaseName){
        return success(dataSourceFeignService.getAllColumnByTableName(id,tableName,dataBaseName));
    }

    /**
     * 模糊分页查询
     *
     * @param weight        任务权重级别 0紧急任务 1高 2中 3低
     * @param name          任务名称
     * @param status        任务状态
     * @param page          当前页
     * @param size          每页显示条数
     * @return   部分数据(id,NAME,weight,status,proportion任务完成度)
     */
    @GetMapping("/taskSearch")
    public R taskSearch(@Param("weight") Integer weight,
                        @Param("name") String name,
                        @Param("status") Integer status,
                        @RequestParam(value = "page",defaultValue = "1") Integer page,
                        @RequestParam(value = "size",defaultValue = "10") Integer size) {
        PageInfo<EtlTaskInfo> search = etlTaskInfoService.taskSearch(weight, name, status, page, size);
        return success(search);
    }

    /**
     * 删除任务
     *
     * @param
     * @return 删除结果
     */
    @DeleteMapping("/deleteById/{id}")
    public R deleteById(@PathVariable("id") String id){
        //根据任务Id获取任务
        EtlTaskInfo etlTaskInfo = etlTaskInfoService.getById(id);
        //判断当前任务是否为执行中
        if(etlTaskInfo.getStatus()==STATUS){
            return success(false);
        }else{
            return success(etlTaskInfoService.deleteById(id));
        }
    }

    /**
     *  任务增加
     * @param etlTaskInfo
     * @return
     */
    @PostMapping("/createTask")
    public R createTask(@RequestBody EtlTaskInfo etlTaskInfo){
        int byNameNotRepeat = etlTaskInfoService.findByNameNotRepeat(etlTaskInfo.getName());

        if(byNameNotRepeat>0){
            return success("任务名称重复");
        }
        if(etlTaskInfo.getWeight()==null){
            return success("权重不能为空");
        }

        try {
            etlTaskInfoService.createTask(etlTaskInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return success(false);
        }
        return success(true);
    }

    @PostMapping("saveTaskProcess")
    public R saveTaskProcess(@RequestBody String taskProcessJson){
        parseProcessJsonService.parseJson(taskProcessJson);
        return success(true);
    }

    @GetMapping("executeTask")
    public R executeTask(@RequestParam(value = "taskId") String taskId) throws CustomException {
        String taskSql = parseProcessJsonService.getTask(taskId);
        return success(taskSql);
    }
}